const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const {
    CleanWebpackPlugin
} = require('clean-webpack-plugin')
const ZonnarPlugin = require('../src/plugins/index.js')

module.exports = {
    mode: 'production',
    // 代表上下文的执行路径。
    context: path.resolve(process.cwd()),
    entry: {
        home: './src/react/home.js',
        shared: ['react', 'react-dom'],
        catalog: {
            import: './src/react/catalog.js',
            filename: './catalog.js',
            dependOn: 'shared',
        },
        personal: {
            import: './src/react/personal.js',
            filename: './personal.js',
            dependOn: 'shared',
            chunkLoading: 'jsonp',
            //   layer: 'name of layer', // set the layer for an entry point
        },
    },
    output: {
        clean: {
            keep: /ext\.txt/
        }, // 在生成文件之前清空 output 目录
        sourcePrefix: '\t',

        path: path.resolve(process.cwd(), './dist02'),
        scriptType: 'module',
        filename: "[name].[chunkhash:8].js",
        chunkFilename: "[name].chunk.[chunkhash:8].js"
    },
    // optimization: {
    //     runtimeChunk: {
    //         name: 'runtime',
    //     },
    // },

    resolve: {
        // 要解析的文件的扩展名
        extensions: [".js", ".jsx", ".json"],
    },
    module: {
        rules: [{
            test: /\.(js|jsx)$/,
            use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env', '@babel/preset-react']
                    }
                },

            include: path.resolve(process.cwd(), './src/react'),
            // exclude: path.resolve(process.cwd(), './node_modules/')
        }]
    },
    plugins: [
        new webpack.ProgressPlugin(),
        new CleanWebpackPlugin(),
        new HTMLWebpackPlugin({
            template: './public/index.html'
        }),
        new ZonnarPlugin({
            outputFile: 'assets.md'
        }),

    ]
}